package com.lanou3g.web;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanou3g.bean.Flats;
import com.lanou3g.service.FlatsService;
import com.lanou3g.util.Result;

@Controller
@RequestMapping("/flats")
public class FlatsController {
	
	@Autowired
	private FlatsService flatsService;
	
	//进入公寓列表页面
	@RequestMapping("/flatsList")
	public String flatsList() {
		return "flats/flatsList";
	}
	
	//获取公寓列表
	@ResponseBody
	@RequestMapping("/getFlatsList")
	public Result getFlatsList(Integer page, Integer limit) {
		return flatsService.getFlats(page, limit);
	}
	
	//进入添加公寓页面
	@RequestMapping("/addFlats")
	public String addFlats() {
		return "flats/addFlats";
	}
	
	//根据公寓名称检查公寓是否存在
	@ResponseBody
	@RequestMapping("/checkFlatsName/{flatsName}")
	public Result checkFlatsName(@PathVariable("flatsName")String flatsName) {
		return flatsService.getFlatsByName(flatsName);
	}
	
	//上传公寓图片
	@ResponseBody
	@RequestMapping("/singleUpload")
	public Map<String, Object> singleUpload(MultipartFile file, HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		//原始文件的名字(含后缀)
		String originalFileName = file.getOriginalFilename();
		//找到最后一个点的下标
		int indexOfDot = originalFileName.lastIndexOf(".");
		//获取原始文件的后缀
		String suffix = originalFileName.substring(indexOfDot);
		
		//生成一个唯一的文件名
		String fileName = UUID.randomUUID() + suffix;
		//虚拟文件路径,用于把路径存储到数据库中
		String filePath = req.getServletContext().getContextPath() + "/files/" + fileName;
		System.out.println("虚拟路径是:" + filePath);
		//真实文件路径,用于存储图片文件
		String realPath = req.getServletContext().getRealPath("") + "/files/" + fileName;
		System.out.println("真实路径是:" + realPath);
		try {
			file.transferTo(new File(realPath));
			map.put("image", filePath);
			map.put("code", 0);
			map.put("msg","上传成功!");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg","上传失败!");
			
			e.printStackTrace();
		}
		return map;
	}
	
	//添加公寓
	@ResponseBody
	@RequestMapping("/insertFlats")
	public Result insertFlats(Flats flats) {
		return flatsService.insertFlats(flats);
	}
	
	//进入公寓编辑页面
	@RequestMapping("/editFlats/{id}")
	public String editFlats(@PathVariable("id")Integer id,HttpSession session) {
		Flats flats = flatsService.getFlatsById(id);
		session.setAttribute("aFlats", flats);
		return "flats/editFlats";
	}
	
	//更新公寓信息
	@ResponseBody
	@RequestMapping("/updateFlats")
	public Result updateFlats(Flats flats) {
		return flatsService.updateFlats(flats);
	}
	
	//删除公寓
	@ResponseBody
	@RequestMapping("/deleteFlats/{id}")
	public Result deleteFlats(@PathVariable("id") Integer id) {
		return flatsService.deleteFlatsById(id);
	}
	
}
